package com.jinlejia.erp.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ItemPushMoneyHeaderBinding;
import com.jinlejia.erp.databinding.ItemPushMoneyListLayoutBinding;
import com.jinlejia.erp.utils.DateUtil;

import java.util.ArrayList;
import java.util.Map;


/**
 * 提成明细Adapter
 */
public class ListPushMoneyAdapter extends YfListAdapter<Map<String, Object>> {

    private Context context;

    public ListPushMoneyAdapter(ArrayList<Map<String, Object>> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_push_money_list_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);

        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, final int i) {
        ItemPushMoneyListLayoutBinding binding = DataBindingUtil.getBinding(viewHolder.itemView);
        Map<String,Object> map = mData.get(i);
        binding.projectName.setText( String.valueOf( map.get("address") ) );
        //
        double amount =  Double.valueOf( map.get("amount").toString() );
        if( amount < 0 ){
            binding.acount.setText( String.valueOf(amount) );
            binding.acount.setTextColor(Color.parseColor("#008000"));
        }else{
            binding.acount.setText( "+"+map.get("amount") );
            binding.acount.setTextColor(Color.parseColor("#FF0000"));
        }
        //成交类型
        switch (String.valueOf( map.get("tradeType") )){
            case "ershoufang":
                binding.cjType.setText("[二手房]");
                break;
            case "xinfang":
                binding.cjType.setText("[一手房]");
                break;
            case "zulin":
                binding.cjType.setText("[租房]");
                break;
            case "dianzi":
                binding.cjType.setText("[垫资]");
                break;
        }

        //提成类型
        String commissionType="";
        switch ( String.valueOf(map.get("commissionType")) ){
            case "pin":
                commissionType = "个人提成";
                break;
            case "trainingMangerPin":
                commissionType = "带训提成";
                break;
            case "developTrainingManagerPin":
                commissionType = "培育带训经理提成";
                break;
            case "developCityPartnerPin":
                commissionType = "培育城市合伙人提成";
                break;
            case "developCityDeputyManagerPin":
                commissionType = "培育城市副总经理提成";
                break;
            case "developCityManagerPin":
                commissionType = "培育城市总经理提成";
                break;
            case "developPin":
                commissionType = "培育分成人提成";
                break;
        }
        binding.tjType.setText( commissionType.concat("   ").concat( DateUtil.getTime( map.get("creatTime").toString(),"MM-dd" ) ) );
        binding.getRoot().setTag(mData.get(i));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_push_money_header, parent, false);
        return new HeaderViewHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindHeaderViewHolder(holder, position);
        ItemPushMoneyHeaderBinding binding = DataBindingUtil.getBinding(holder.itemView);
        Map<String,Object> header = (Map<String, Object>) mHeaders.get(0);
        binding.time.setText(DateUtil.getTime((long)header.get("createTime"),"yyyy-MM"));
        double commissionSum = Double.parseDouble(header.get("commissionSum").toString());
        if( commissionSum < 0 ){
            binding.mounthAmount.setText("" + header.get("commissionSum"));
        }else {
            binding.mounthAmount.setText("+" + header.get("commissionSum"));
        }

    }

    static final class HeaderViewHolder extends RecyclerView.ViewHolder {

        public HeaderViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }

    static final class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }


}
